package com.yc.baseapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * author: yc
 * description: ${description}
 * date: 2021/10/22 16:13
 */
public class TestEncode {

    //utf8 转中文
    public void m1() {
        String jsonParams = "{\"contractRoot\":{\"body\":{\"createTime\":\"20211022154348000\",\"extCode\":\"\",\"orderItemId\":\"901133458012737537\",\"price\":0,\"quantity\":1,\"rightsId\":\"201911260128\",\"serverNum\":\"19802021563\",\"skuCode\":\"86475\",\"skuId\":\"100327263\",\"skuName\":\"18\\xE5\\x85\\x83\\xE6\\xA0\\x87\\xE5\\x87\\x86\\xE6\\xB4\\x97\\xE8\\xBD\\xA6\\xE4\\xBB\\xA3\\xE9\\x87\\x91\\xE5\\x88\\xB8\"},\"head\":{\"apiId\":\"200047\",\"channelCode\":\"201911260128\",\"reqTime\":\"20211022154227627\",\"sign\":\"6e052707fc85da9bfcda08fadcc28813\",\"transactionId\":\"cada7b0d-63c5-417e-995b-3948611a173f4647\",\"version\":\"1.0\"}}}";
        try {
            System.out.println(hex2UTF8(jsonParams));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    //带utf8编码的字符串转json
    public void m2() {
        String jsonParams = "{\"contractRoot\":{\"body\":{\"createTime\":\"20211022154348000\",\"extCode\":\"\",\"orderItemId\":\"901133458012737537\",\"price\":0,\"quantity\":1,\"rightsId\":\"201911260128\",\"serverNum\":\"19802021563\",\"skuCode\":\"86475\",\"skuId\":\"100327263\",\"skuName\":\"18\\xE5\\x85\\x83\\xE6\\xA0\\x87\\xE5\\x87\\x86\\xE6\\xB4\\x97\\xE8\\xBD\\xA6\\xE4\\xBB\\xA3\\xE9\\x87\\x91\\xE5\\x88\\xB8\"},\"head\":{\"apiId\":\"200047\",\"channelCode\":\"201911260128\",\"reqTime\":\"20211022154227627\",\"sign\":\"6e052707fc85da9bfcda08fadcc28813\",\"transactionId\":\"cada7b0d-63c5-417e-995b-3948611a173f4647\",\"version\":\"1.0\"}}}";
        JSONObject j1 = JSONObject.parseObject(jsonParams);
        System.out.println("---------");

    }


    public String hex2UTF8(String hexStr) throws UnsupportedEncodingException {
        return URLDecoder.decode(hexStr.replaceAll("\\\\x", "%"), StandardCharsets.UTF_8.name());
    }

    public String hex2GBK(String hexStr) throws UnsupportedEncodingException {
        return URLDecoder.decode(hexStr.replaceAll("\\\\x", "%"), "gbk");
    }

    public static void main(String[] args) throws Exception{
        TestEncode te = new TestEncode();
//        te.m1();
        te.m2();
    }
}
